import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CustomPagination extends SwiperPlugin {
  @override
  Widget build(BuildContext context, SwiperPluginConfig config) {
    return ConfigProvider(
        config: config, child: const CustomPaginationWidget());
  }
}

class CustomPaginationWidget extends StatefulWidget {
  const CustomPaginationWidget({super.key});

  @override
  State<StatefulWidget> createState() => CustomPaginationWidgetState();
}

class CustomPaginationWidgetState extends State<CustomPaginationWidget> {
  late SwiperPluginConfig config;

  late double page;

  @override
  void didChangeDependencies() {
    config = ConfigProvider.of(context)!.config;
    page = config.pageController!.page!;
    config.pageController!.addListener(() {
      setState(() {
        page = config.pageController!.page!;
      });
    });
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    final List<Widget> list = [];
    final double baseWidth = 6.w;
    final double ratio = 3.w;
    final int itemCount = config.itemCount;
    final int floor = page.floor();
    final int ceil = page.ceil();
    final int preIndex = floor % itemCount;
    final int nextIndex = ceil % itemCount;
    final double prePercent = page - preIndex;
    final double nextPercent = 1 - prePercent;

    //final double baseOpacity = 1.w;
    //final double offsetOpacity = 1.0 - baseOpacity;

    for (int i = 0; i < itemCount; ++i) {
      double width;
      //double opacity;
      if (i == preIndex) {
        width = baseWidth * (1.0 + ratio * nextPercent);
        //opacity = baseOpacity + offsetOpacity * nextPercent;
      } else if (i == nextIndex) {
        width = baseWidth * (1.0 + ratio * prePercent);
        //opacity = baseOpacity + offsetOpacity * prePercent;
      } else {
        width = baseWidth;
        //opacity = baseOpacity;
      }

      list.add(Container(
        key: Key('customPagination_$i'),
        margin: EdgeInsets.symmetric(horizontal: 3.w),
        child: ClipRRect(
          borderRadius: BorderRadius.horizontal(
              left: Radius.circular(3.sp), right: Radius.circular(3.sp)),
          child: Container(
            color: width != baseWidth
                ? const Color(0xff5C6DFF)
                : const Color(0xff5C6DFF).withOpacity(0.6),
            width: width,
            height: 6.w,
          ),
        ),
      ));
    }
    return Container(
        alignment: Alignment.bottomCenter,
        padding: EdgeInsets.only(bottom: 18.w),
        child: Row(mainAxisSize: MainAxisSize.min, children: list));
  }

  @override
  void dispose() {
    config.pageController!.dispose();
    super.dispose();
  }
}

class ConfigProvider extends InheritedWidget {
  const ConfigProvider({required this.config, required super.child, super.key});
  final SwiperPluginConfig config;
  //@override
  //final Widget child;

  static ConfigProvider? of(BuildContext context) =>
      context.dependOnInheritedWidgetOfExactType<ConfigProvider>();

  @override
  bool updateShouldNotify(ConfigProvider oldWidget) {
    return oldWidget.config != config;
  }
}
